package TUILag;
import CtrLag.*;
import ModelLag.*;

/**
 * Handles the Person TUI in the system
 * 
 * @author (Lackshan, Mikkel, Nick, patrick, Simon, Yousef) 
 * @project (Tema design)
 * @version (2014.11.04)
 */
public class PersonTUI
{
    private PersonCtr pCtr;
    private InputReader reader;
    
    /**
     * Constructor for objects of class PersonUI
     */
    public PersonTUI(){
        pCtr = new PersonCtr();
        reader = InputReader.getInstance();
    }

    /**
     * Runs the personTUI 
     */
    public void run(){
        printMenu();
        boolean finished = false;
        while(!finished) {
            String choise = reader.getInput();
            switch(choise){
                case "1":
                    addNewFriend();
                    break;
                case "2":
                   updateFriend();
                    break;
                case "3":
                   findFriend();
                    break;
                case "4":
                   deleteFriend();
                    break;
                case "5":
                    finished = true;
                    break;
                default:
                    System.out.println("Not a command");
                    break;
            }
            printMenu();
        }
    }
    
    /**
     * Prints the Person menu to the terminal
     */
    private void printMenu(){
        System.out.println("** Friends **");
        System.out.println("1. Add new friend");
        System.out.println("2. Update friend");
        System.out.println("3. Find friend");
        System.out.println("4. Delete friend");
        System.out.println("5. Main menu");
    }
    
    /**
     * Adds a new friend to the system
     */
    private void addNewFriend(){
        // get the info
        String name = reader.getInputWithLabel("Name");
        String address = reader.getInputWithLabel("Address");
        String zip = reader.getInputWithLabel("Zip");
        String city = reader.getInputWithLabel("City");
        String phone = reader.getInputWithLabel("Phone");
        
        // Send the message to the controller:
        pCtr.createPerson(name, address, zip, city, phone);
        // print message to the terminal 
        System.out.println("Friend was created!");
    }
    
    /**
     * Update a friend based on phone number
     */
    private void updateFriend(){
        String phone = reader.getInputWithLabel("phone");

        try{
            // find the person in the system
            Person p = pCtr.findPerson(phone);
            
            // get new values
            System.out.println("Edit: " + p.getName());
            String newName = reader.getInputWithLabel("New name");
            String newAddress = reader.getInputWithLabel("New address");
            String newZip = reader.getInputWithLabel("New zipcode");
            String newCity = reader.getInputWithLabel("New city");
            String newPhone = reader.getInputWithLabel("New Phone");
            
            // send the message to the controller
            pCtr.updatePerson(p, newName, newAddress, newZip, newCity, newPhone);
            // print message to the terminal 
            System.out.println("Friend was updated!");
        }catch(Exception e){
            // if no person where found
            System.out.println("Friend not found!");
        }
    }
    
    /**
     * Prints a friend´s information to the terminal
     * 
     */
    private void findFriend(){
        // the phone number of the friend
        String phone = reader.getInputWithLabel("phone");
        try{
            Person p = pCtr.findPerson(phone);
            System.out.println("Friend: " + p.getName());
            System.out.println("Address: " + p.getAddress());
            System.out.println("Zip: " + p.getZip());
            System.out.println("City: " + p.getCity());
            System.out.println("Phone: " + p.getPhone());
        }catch(Exception e){
            System.out.println("Friend not found!");
        }
    }
    
    /**
     * Delete a friend from the system
     */
    private void deleteFriend(){
        String phone = reader.getInputWithLabel("phone");

        try{
            Person p = pCtr.findPerson(phone);
            pCtr.deletePerson(phone);
            System.out.println("Friend was deleted!");
        }catch(Exception e){
            System.out.println("Friend not found!");
        }
    }
}
